﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.ReceptionGoods
{
    public partial class GoodsList : Form
    {
        public GoodsList()
        {
            InitializeComponent();
        }
        private DataSets.Util.vwGoodsDetailedDataTable Data
        {
            get
            {
                return this.util.vwGoodsDetailed;
            }
        }
        private DataView View
        {
            get
            {
                return this.Data.DefaultView;
            }
        }
        private Int32 Count
        {
            get
            {
                return this.View.Count;
            }
        }
        private Int32  _goodsId;

        public Int32  GoodsId
        {
            get 
            {
                Int32 tmp = _goodsId;
                _goodsId = Int32.MinValue;
                return tmp; 
            }
            set { _goodsId = value; }
        }

        private void GoodsList_Load(object sender, EventArgs e)
        {
            DataSets.UtilTableAdapters.vwGoodsDetailedTableAdapter adapter = new DIProject.DataSets.UtilTableAdapters.vwGoodsDetailedTableAdapter();
            adapter.Fill(this.util.vwGoodsDetailed);
            dgvGoods.RowCount = this.Count;
        }

        private void dgvGoods_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
        {
            Int32 rNum = e.RowIndex;
            Int32 cNum = e.ColumnIndex;
            if (this.Count == 0)
            {
                e.Value = string.Empty;
                return;
            }
            e.Value = this.View[rNum][cNum];
        }

        private void dgvGoods_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Int32 rNum = e.RowIndex;
            DataRowView drv = this.View[rNum];
            DataSets.Util.vwGoodsDetailedRow row = drv.Row as DataSets.Util.vwGoodsDetailedRow;
            this.GoodsId = row.Id;
            this.DialogResult = DialogResult.OK;
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (dgvGoods.SelectedRows.Count < 1)
            {
                MessageBox.Show("Требуется выбрать товар!");
                return;
            }
            this.GoodsId = (Int32)dgvGoods.SelectedRows[0].Cells[0].Value;
            this.DialogResult = DialogResult.OK;
        }

        private void btnFiltr_Click(object sender, EventArgs e)
        {
            string str = txbFiltr.Text;
            if (string.IsNullOrEmpty(str))
            {
                this.View.RowFilter = string.Empty;
                

            }
            else
            {
                this.View.RowFilter = "FullName LIKE '*" + str + "*'";
            }
            dgvGoods.Rows.Clear();
            dgvGoods.RowCount = this.Count;
        }
    }
}